/**
 * 资料列表适配器
 */
package com.xiaomuwu.android.ui.shared

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.xiaomuwu.android.R
import com.xiaomuwu.android.XiaoMuWuApplication
import com.xiaomuwu.android.logic.model.Res
import com.xiaomuwu.android.ui.Displayer
import com.xiaomuwu.android.ui.ForSearch

class ResListAdapter(private val resList:List<Res>) :
    RecyclerView.Adapter<ResListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        val resName:TextView=view.findViewById(R.id.res_Name)   // 资料名
        var resTags:TextView=view.findViewById(R.id.res_tags)   // 标签
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(parent.context)
            .inflate(R.layout.res_item,parent,false)
        val viewHolder=ViewHolder(view)

        // 资料列表项点击监听
        viewHolder.itemView.setOnClickListener{
            val position=viewHolder.adapterPosition
            ForSearch.resID=resList[position].id    // 设置资料详情页ID
//            Displayer.page=0  // test:实现从用户详情页返回后滚动到原来的浏览位置
            Displayer.pprevPageStatus=Displayer.prevPageStatus
            Displayer.prevPageStatus=Displayer.pageStatus
            Displayer.pageStatus="ResDetail"
            Displayer.resName=resList[position].name    // 更新资料名
            Displayer.resTags=resList[position].tags    // 更新标签

            // 发送显示资料详情广播
            val intent= Intent("com.xiaomuwu.android.ShowResDetail")
            XiaoMuWuApplication.context.sendBroadcast(intent)
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val res=resList[position]
        holder.resName.text=res.name    // 设置资料名
        holder.resTags.text=res.tags    // 设置标签
    }

    override fun getItemCount(): Int =resList.size

}